<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $user_id
 * @property string $username
 * @property string $password
 * @property string $name
 * @property string $phone
 * @property string $address
 * @property string $email
 * @property string $staus
 */
class User extends CActiveRecord {

    public $repassword;
    public $newpassword;
    public $oldpassword;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password, name, phone, address, email', 'required','message' =>'{attribute} không dược bỏ trống'),
            array('username, password, name, phone, address, email', 'length', 'max' => 255),
            array('repassword','required','on' => 'create'),
            array('username', 'unique', 'message' => 'tài khoản đăng nhập này đã có người dùng'),
            array('email', 'unique', 'message' => 'địa chỉ email  này đã có người sử dụng'),
            array('repassword', 'compare', 'compareAttribute'=>'password','message' => 'Xác nhận mật khẩu chưa đúng','on' => 'create'),
            array('phone', 'match', 'pattern'=>'/[0-9]$/u', 'message'=>'số điện thoại chưa hợp lệ.'),
            array('status','safe'),
            array('email','email','message' => 'địa chỉ email không hợp lệ'),
            array('newpassword,oldpassword', 'required','message' =>'{attribute} không dược bỏ trống','on' => 'change_pass'),
            array('repassword', 'compare', 'compareAttribute'=>'newpassword','message' => 'Xác nhận mật khẩu chưa đúng','on' => 'change_pass'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('user_id, username, password, name, phone, address, email, staus', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'user_id' => 'User',
            'username' => 'Tên tài khoản',
            'password' => 'Mật khẩu',
            'repassword' => 'Xác nhận mật khẩu',
            'name' => 'Họ và tên',
            'phone' => 'Số điện thoai',
            'address' => 'Địa chỉ',
            'email' => 'Email',
            'staus' => 'Staus',
            'oldpassword' => 'Mật khẩu cũ',
            'newpassword' => 'Mật khẩu mới',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('user_id', $this->user_id);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('staus', $this->staus, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }
    
    /**
     *
     * @param type $password
     * @return type 
     */
    public function validatePassword($password) {
        return NewHashPassword::verifyPassword($password, $this->password);
    }

    /**
     *
     * @param type $password
     * @return type 
     */
    public function hashPassword($password) {
        return NewHashPassword::hashPassword($password);
    }

}
